
local hard_list = load_excel('数据投放.xlsx','难度系数')
local unit_data = load_excel('数据投放.xlsx','怪物数据')

for k,v in pairs(unit_data) do
    v.name = k
    v.skill = v['技能'] and split_string(v['技能'],',')
    v.types = v['类型'] and split_string(v['类型'],',')
    ac.lni('unit',k,v)
end

local data_list = {}

--乘区最大倍率
local function get_mul(rate,count)
    local count = count or 5
    if count<1 then
        return 1
    end
    local min = 10
    local base = math.ceil(rate/min)
    local num = math.floor(base/count)
    local mul = 1 + (rate - num*count*min)*0.01
    
    for a=1,count do
        mul = mul * (1 + num*0.1)
    end
    
    mul = math.ceil(mul*10)*10 - 100
    return mul
end

local function init_data_list(data)

    for a=1,6 do
        --基础属性
        local sx1 = data['属性'..(a-1)] or 100
        local sx2 = data['属性'..a] or 100

        --乘区数据
        local ad1 = data['乘区'..(a-1)] or 100
        local ad2 = data['乘区'..(a-1)] or 100

        --技能倍率
        local sp1 = data['倍率'..(a-1)] or 100
        local sp2 = data['倍率'..(a-1)] or 100

        --进度
        for b=1,5 do
            local sx3 = sx1 + (sx2 - sx1) * b / 5
            local ad3 = ad1 + (ad2 - ad1) * b / 5
            local sp3 = sp1 + (sp2 - sp1) * b / 5
            for c=1,60 do
                local sx4 = sx1 + (sx3 - sx1) * c / 60
                data_list[#data_list + 1] = {
                    ['攻击'] = sx4,
                    ['生命'] = sx4 * 10,
                    ['乘区'] = ad3,
                    ['倍率'] = sp3,
                }
            end
        end
    end
    

end

local hard_data = hard_list[1]
init_data_list(hard_data)

ac.game:event '游戏-设置难度'(function(_,hard)
    hard_data = hard_list[hard] or hard_list[1]
    init_data_list(hard_data)
end)



function ac.unit.__index:set_enemy_level(level)
    local name = self.name
    if type(level)=='string' then
        name = level
        level = nil
    end
    local data1 = unit_data[name]
    local level = level or data1['默认等级']
    local data2 = data_list[level]
    if not data1 then
        log.error('错误怪物类型',name)
    end
    if not data2 then
        log.error('错误怪物等级',level)
        return
    end
    local time = data1['击杀时间'] or 1
    local rate = data1['每秒输出'] or -20
    
    local ad = get_mul(data2['乘区'] or 0)
    local gj = data2['攻击']
    local sm = data2['生命']
    --速度（攻速/冷却）
    local sd = 100
    if ad>400 then
        sd = 500
        ad = ad - 400
    else
        sd = sd + ad
        ad = 0
    end

    local hx = gj * 0.2

    local zgj = gj
    local dps = sm * rate / 100 + hx
    if rate<0 then
        dps = - hx * rate / 100
    end
    
    local gap = data1['攻击间隔'] or 2
    if not self:is_type('普通') then
        gap = 1
    end
    local atk = dps*gap
    local smz = zgj*time * (1 + ad/100)

    if self:is_type('领主') then
        --print(('打点%s = %sX%s(%s) 回血%s(%s%%)'):format(zgj,gj,ad,data2['乘区'] or 0,hxz,hxz*100/smz))
    end
    self.enemy_level = level
    self.zdl = zdl
    self:set('攻击间隔',gap)
    self:set('基础攻击',atk)
    self:set('生命上限',smz)
    self:set('基础护甲',sd - 100)

    self:add_type(data1.types)

    if (self:is_type('精英') or self:is_type('挑战')) and unit_add_life2 then
        unit_add_life2(self)
    elseif self:is_type('领主') and unit_add_life2 then
        unit_add_life2(self,2)
    elseif unit_add_life2 then
        -- self.ui_title = ''
        -- unit_add_life2(self)
    end
    return self
end


ac.game:event '单位-加载数据'(function(_,u)
    local lv = u['默认等级']
    if lv then
        u:set_enemy_level(u['默认等级'])
    end
end)


-- local test_data = {}
-- for k,v in pairs(unit_data) do
--     local u = ac.player[16]:create_unit(v.name)
--     u:remove()
--     table.insert(test_data,{
--         art = u:get_art(),
--         name = u.name,
--         tip = '点击召唤',
--     })
-- end

-- table.sort(test_data,function(a,b)
--     return a.name:hash()>b.name:hash()
-- end)

-- ac.game:notify('测试-注册数据','测试单位',test_data,function(_,player,name)
--     local unit = player:add_enemy(name)
--     if ac.enemy then
--         unit:set_enemy_level(ac.enemy.level)
--     end
-- end)